package com.newtouch.cq.base.starter.business.controller;


import com.newtouch.cq.base.starter.business.entity.SysOrganization;
import com.newtouch.cq.base.starter.business.model.dto.SysOrganizationAddDTO;
import com.newtouch.cq.base.starter.business.model.dto.SysOrganizationUpdDTO;
import com.newtouch.cq.base.starter.business.model.vo.OrgTreeModel;
import com.newtouch.cq.base.starter.business.service.IBaseSysOrganizationService;
import com.newtouch.cq.lang.annotation.NoLog;
import com.newtouch.cq.lang.result.MccObjectResult;
import com.newtouch.cq.lang.result.MccResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统组织表 前端控制器
 * </p>
 *
 * @author Alex
 * @since 2021-08-02
 */
@RestController
@RequestMapping("/sys/organizations")
@Api(tags = "组织相关接口")
public class BaseSysOrganizationController {

    @Autowired
    private IBaseSysOrganizationService organizationService;


    /**
     * 返回树形菜单集合
     *
     * @return 树形菜单
     */
    @NoLog
    @GetMapping(value = "tree")
    @ApiOperation("返回树形菜单集合")
    public MccObjectResult<List<OrgTreeModel>> getTree() {
        return organizationService.selectListTree();
    }

    /**
     * 通过组织标识查询详情
     *
     * @param id ID
     * @return SysDept
     */
    @NoLog
    @GetMapping("{id}")
    @ApiOperation("通过组织标识查询详情")
    @ApiImplicitParam(name = "id", value = "组织标识", dataTypeClass = Long.class,paramType = "path")
    public MccObjectResult<SysOrganization> get(@PathVariable Long id) {
        return MccObjectResult.success(organizationService.getById(id));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @ApiOperation("删除")
    @DeleteMapping("{id}")
    @PreAuthorize("hasAuthority('sys_org_del')")
    @ApiImplicitParam(name = "id", value = "组织标识", dataTypeClass = Long.class,paramType = "path")
    public MccResult delete(@PathVariable long id) {
        organizationService.deleteById(id);
        return MccResult.SUCCESS;
    }

    /**
     * 添加
     *
     * @param dto 组织
     * @return success/false
     */
    @ApiOperation("添加")
    @PostMapping
    @PreAuthorize("hasAuthority('sys_org_add')")
    public MccResult add(@RequestBody SysOrganizationAddDTO dto) {
        organizationService.add(dto);
        return MccResult.SUCCESS;
    }

    /**
     * 编辑
     *
     * @param dto 实体
     * @return success/false
     */
    @ApiOperation("编辑")
    @PutMapping("{id}")
    @PreAuthorize("hasAuthority('sys_org_upd')")
    @ApiImplicitParam(name = "id", value = "组织标识", dataTypeClass = Long.class,paramType = "path")
    public MccResult update(@PathVariable long id,@RequestBody SysOrganizationUpdDTO dto) {
        organizationService.upadte(id,dto);
        return MccResult.SUCCESS;
    }

}
